package com.youhome.mobile.controller;

import com.youhome.common.config.OSSConfig;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.mobile.dto.DTOClientCreate;
import com.youhome.mobile.dto.DTOClientUpdate;
import com.youhome.mobile.utils.JwtTokenUtil;
import com.youhome.service.model.BClient;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.BUser;
import com.youhome.service.service.BClientService;
import com.youhome.service.service.BUserService;
import com.youhome.service.service.CounselorService;
import com.youhome.service.service.UploadService;
import com.youhome.utils.DateUtils;
import com.youhome.utils.ValidationUtils;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chen365 on 2018/09/17.
 */
@Api(tags = "客户")
@RestController
@RequestMapping(value = "/clients")
public class ClientController {
    @Autowired
    private BClientService clientService;
    @Autowired
    private UploadService uploadService;
    @ApiOperation(value = "列表", notes = "列表", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "")
    public Object list(HttpServletRequest request, Integer pageNumber, Integer pageSize) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        return new ResultPage<>(ResultCode.SUCCESS, clientService.selectByBean(phone, pageNumber, pageSize));
    }

    @ApiOperation(value = "详情", notes = "详情", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{clientId}")
    public Object details(@PathVariable Long clientId) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, clientService.selectByClientIdOfMobile(clientId));
    }

    @ApiOperation(value = "新增", notes = "新增", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "")
    public Object add(HttpServletRequest request, @ApiParam @Validated DTOClientCreate dtoClientCreate) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        clientService.create(phone, modelMapper.map(dtoClientCreate, BClient.class));
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "修改", notes = "修改", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping(value = "/{clientId}")
    public Object update(HttpServletRequest request, @PathVariable Long clientId, @ApiParam @Validated DTOClientUpdate dtoClientUpdate) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        dtoClientUpdate.setClientId(clientId);
        clientService.updateOfMobile(phone, modelMapper.map(dtoClientUpdate, BClient.class));
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/{clientId}")
    public Object delete(HttpServletRequest request, @PathVariable Long clientId) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        clientService.deleteByClientId(phone, clientId);
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "上传头像", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "data", value = "base64 数据", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "ext", value = "图片后辍名(jpg,png)", required = true, paramType = "query")
    })
    @PostMapping(value = "/photo/upload")
    public Object uploadPhoto(String data, String ext) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, uploadService.upload(data, ext));
    }
}
